<template>
  <div class="register">
    <div class="container">
      <van-form @submit="onSubmit">
        <div class="flex">
          <van-icon name="https://auth.zto.com/m/img/icon1.png" />
          <van-field
            v-model="telenumber"
            name="teleNumber"
            label=""
            placeholder="请输入手机号"
          />
          <van-icon
            class="close"
            name="https://auth.zto.com/m/img/delete.png"
            @click="clear"
          />
        </div>

        <div class="flex">
          <van-icon name="https://auth.zto.com/m/img/icon2.png"></van-icon>
          <van-field
            v-model="codemove"
            name="codeMove"
            label=""
            placeholder="请输入动态验证码"
          />
          <div class="codeMoveMess" @click="getTeleMoveCode">
            {{ codemovemess }}
          </div>
        </div>

        <div class="flex">
          <van-icon name="https://auth.zto.com/m/img/icon2.png"></van-icon>
          <van-field
            v-model="code2"
            name="code"
            label=""
            placeholder="请输入手机验证码"
          />
          <van-button
            class="getCode"
            plain
            hairline
            type="info"
            native-type="button"
            @click="getTeleCode"
            >{{ codeBtnMessage }}</van-button
          >
        </div>

        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >提交</van-button
          >
        </div>
        <footer />
      </van-form>
      <p class="footMess">
        为了准确地向您推送最新消息，防止部分用户恶意下单，需要绑定您的常用手机号，如已是会员请直接登录
      </p>
    </div>
  </div>
</template>

<script>
import { getLoginData, getMoveCode, getCode } from '@/api/login'
import { Toast } from 'vant'
export default {
  name: 'Register',
  data() {
    return {
      telenumber: '',
      codemove: '',
      codemovemess: '',
      code: '',
      code2: '',
      codeBtnMessage: '获取验证码',
      login: [],
      flag: true,
      // 用于判断是否需要rap2验证
      testFlag: false,
      rule: /^1(3[0-9]|4[01456879]|5[0-3,5-9]|6[2567]|7[0-8]|8[0-9]|9[0-3,5-9])\d{8}$/
    }
  },
  created() {
    // 登录信息数据
    getLoginData().then((res) => {
      this.login = res.data.data
    })
    // 动态验证码
    getMoveCode().then((res) => {
      this.codemovemess = res.data.code
    })
  },
  methods: {
    onSubmit(values) {
      if (values.teleNumber === '' || values.codeMove === '' || values.code === '') {
        Toast({
          message: '手机号或验证码不能为空',
          position: 'bottom'
        })
      } else if (this.rule.test(values.teleNumber) && values.codeMove.toUpperCase() === this.codemovemess.toUpperCase() && values.code.toUpperCase() === this.code.toUpperCase()) {
        if (localStorage.getItem('loginInfo')) {
          // 查找是否含有该手机号
          let tempFlag = false
          const temp = JSON.parse(localStorage.getItem('loginInfo'))
          for (let i = 0; i < temp.length; i++) {
            if (temp[i].teleNumber === values.teleNumber) {
              Toast({
                message: '已存在该用户 请登录',
                position: 'bottom'
              })
              tempFlag = false
              break
            } else {
              tempFlag = true
            }
          }
          if (tempFlag === true) {
            temp.push(values)
            localStorage.setItem('loginInfo', JSON.stringify(temp))
            Toast({
              message: '注册成功',
              position: 'bottom'
            })
            this.$router.push('/home')
          }
        } else {
          const temp = []
          temp.push(values)
          localStorage.setItem('loginInfo', JSON.stringify(temp))
          Toast({
            message: '注册成功',
            position: 'bottom'
          })
          this.$router.push('/home')
        }
      } else {
        Toast({
          message: '手机号或验证码有误',
          position: 'bottom'
        })
      }
    },
    getTeleCode() {
      if (this.telenumber === '') {
        Toast({
          message: '请输入手机号！',
          position: 'bottom'
        })
      } else if (this.rule.test(this.telenumber)) {
        if (this.flag === true) {
          // 获取验证码
          getCode().then((res) => {
            this.code = res.data.code
            console.log('手机验证码：', res.data.code)
            let temp = 60
            this.flag = false
            // 点击事件失效
            const timer = setInterval(() => {
              this.codeBtnMessage = temp + 's'
              temp--
              if (temp === 0) {
                clearInterval(timer)
                this.codeBtnMessage = '获取验证码'
                // 恢复点击效果
                this.flag = true
              }
            }, 1000)
          })
        } else {
          Toast({
            message: '验证码已发送请勿重复点击',
            position: 'bottom'
          })
        }
      } else {
        Toast({
          message: '手机号格式不正确！',
          position: 'bottom'
        })
      }
    },
    getTeleMoveCode() {
      getMoveCode().then((res) => {
        this.codemovemess = res.data.code
        // console.log('动态验证码：', res.data.code)
      })
    },
    clear() {
      this.telenumber = ''
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 0 0.6rem;
}

.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
  position: relative;
  margin-top: 0.5rem;
  margin-bottom: 1rem;
  .van-icon:not(.close .showMM .hideMM) {
    position: absolute;
    z-index: 1;
    left: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 1.2rem;
  }
  .getCode {
    width: 6.5rem;
    height: 2rem;
  }
  .close {
    font-size: 1.2rem;
  }
  .van-field {
    padding-left: 30px;
    font-size: 0.85rem;
  }
  .van-cell::after {
    border-bottom: 1px solid #333;
    width: 100%;
  }
}

.hideMM,
.showMM {
  display: none;
  position: absolute;
  right: 0;
  img {
    width: 1.3rem;
    height: 1.3rem;
  }
}
.show {
  display: block;
}
.getCode,
.close {
  position: absolute;
  right: 0.6rem;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
}

.codeMoveMess {
  border: 1px solid #000;
  padding: 10px 20px;
  position: absolute;
  right: 0;
  width: 4rem;
  text-align: center;
}

.footMess {
  color: #868686;
  font-size: 0.8rem;
  text-align: center;
  margin-top: 0.5rem;
}
</style>
